import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../../manger/network_utils.dart';
import '../../components/toast_utils.dart';

class FeedbackPage extends StatefulWidget {
   FeedbackPage({super.key});

  @override
  State<FeedbackPage> createState() => _FeedbackPageState();
}

class _FeedbackPageState extends State<FeedbackPage> {

  final TextEditingController _feedbackController = TextEditingController();
  final TextEditingController _contactController = TextEditingController();

  Future<void> _handleSubmit(BuildContext context) async {
    if (_feedbackController.text.trim().isEmpty) {
      if (mounted) {
        ToastUtils.showCenterToast(context, AppLocalizations.of(context)!.jiandanmiaoshu,);
      }
      return;
    }
    
    if (mounted) {
      ToastUtils.showLoadingToast(context);
    }
    
    final response = await NetworkUtils.post(
      RequestCmd.feedBack,
      body: {
        'content': _feedbackController.text,
        'mobile': _contactController.text
      },
    );
    
    ToastUtils.dismissLoadingToast();
    ToastUtils.showCenterToast(context, AppLocalizations.of(context)!.feedback + AppLocalizations.of(context)!.success);
    
    await Future.delayed(Duration(seconds: 2));
    if (mounted) {
      Navigator.popUntil(context, (route) => route.isFirst);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        title: Text(AppLocalizations.of(context)!.feedbacks,style: const TextStyle(fontSize: 17)),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 15),
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: 20, left: 0, right: 0),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12),
              ),
              padding: const EdgeInsets.all(15),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    AppLocalizations.of(context)!.description,
                    style: TextStyle(
                      fontSize: 15,
                      color: Color(0xFF252525),
                    ),
                  ),
                  SizedBox(height: 10),
                  Stack(
                    children: [
                      SizedBox(
                        height: 180,
                        child: TextField(
                          maxLength: 200,
                          maxLines: 999,
                          controller: _feedbackController,
                          onChanged: (text) {
                            // Update character count
                          },
                          decoration: InputDecoration(
                            hintText: AppLocalizations.of(context)!.jiandanmiaoshu,
                            hintStyle: TextStyle(color: Color(0xFF7B7B7B)),
                            contentPadding: EdgeInsets.all(15),
                            filled: true,
                            fillColor: Color(0xFFF5F5F5),
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(12),
                              borderSide: BorderSide.none,
                            ),
                          ),
                          style: TextStyle(
                            fontSize: 14,
                            color: Color(0xFF252525),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 15, bottom: 15),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12),
              ),
              padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
              child: Row(
                children: [
                  Text(
                    AppLocalizations.of(context)!.contactInformation,
                    style: TextStyle(
                      fontSize: 15,
                      color: Color(0xFF252525),
                    ),
                  ),
                  SizedBox(width: 20),
                  Expanded(
                    child: TextField(
                      controller: _contactController,
                      textAlign: TextAlign.right,
                      style: TextStyle(
                        fontSize: 14,
                        color: Color(0xFF252525),
                      ),
                      decoration: InputDecoration(
                        hintText: AppLocalizations.of(context)!.bianyulianxi,
                        hintStyle: TextStyle(color: Color(0xFF7B7B7B)),
                        contentPadding: EdgeInsets.symmetric(vertical: 10, horizontal: 0),
                        border: InputBorder.none,
                        isDense: true,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 30, left: 0, right: 0),
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: Color(0xFF5D5FEE),
                  foregroundColor: Colors.white,
                  textStyle: const TextStyle(fontSize: 15),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20),
                  ),
                  minimumSize: const Size.fromHeight(50),
                ),
                onPressed: () => _handleSubmit(context),
                child: Text(
                  isChineseLocale() ? '${AppLocalizations.of(context)!.submit}${AppLocalizations.of(context)!.feedback}' : AppLocalizations.of(context)!.submit,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}